// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        weibu: {
            type: cc.Node,
            default: null,
        },
        circle: {
            type: cc.Node,
            default: null,

        },

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {},
    blink(node, time) {
        let fadeIn = cc.fadeIn(time);
        let delay = cc.delayTime(0.5);
        let fadeOut = cc.fadeOut(time);
        let seq = cc.sequence(fadeOut, delay, fadeIn);
        node.runAction(seq).repeatForever();
    },

    init: function(x, y, width) {
        this.v2 = cc.v2(x, y);
        this.width = width;
    },
    begin: function() {
        //隐藏病毒的尾部
        console.log('111');
        this.weibu.runAction(cc.fadeOut(0.1));
        this.blink(this.circle, 0.2);

        this.virusAction();
    },
    reset: function() {
        this.node.stopAllActions();
        this.circle.stopAllActions();
    },
    virusAction: function() {

        this.node.stopAllActions();
        // moveTo动作移动的误差会积累
        let x = random(0, this.width);
        let y = random(0, this.width);
        let v2 = cc.v2(this.v2.x + x, this.v2.y + y);
        let seq = cc.sequence(cc.moveTo(1, v2),
            cc.callFunc(function() {
                this.virusAction();
            }.bind(this)));
        this.node.runAction(seq);
    },

    start() {

    },

    // update (dt) {},
});